<template>
  <div class="AddUser">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="ruleForm.userName"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="userNum">
        <el-input v-model="ruleForm.userNum"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="ruleForm.nickName"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <!-- https://blog.csdn.net/weixin_44449838/article/details/113247836 -->
        <!-- 

          value-key="roleId"===> 如果绑定的是对象，则需要这个属性 el-option
         -->
        <el-select
          v-model="ruleForm.role"
          multiple
          collapse-tags
          style="margin-left: 20px"
          value-key="roleId"
          placeholder="请选择"
        >
          <el-option
            v-for="item in roles"
            :key="item.roleId"
            :label="item.roleName"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { userSave } from "../../apis/user";
import { roleList } from "../../apis/role";
import { Message } from "element-ui";
export default {
  name: "AddUser",
  data() {
    return {
      ruleForm: {
        userName: "",
        userNum: "",
        nickName: "",
        password: "",
        role: [],
      },
      rules: {
        userName: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 3, max: 20, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        userNum: [{ required: true, message: "请输入账号", trigger: "change" }],
        nickName: [
          { required: true, message: "请输入昵称", trigger: "change" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      userSave({
        ...this.ruleForm,
        roles: this.ruleForm.role.map((role2) => {
          return { roleId: role2.roleId };
        }),
      })
        .then(() => {
          this.$router.push("/user/index");
          Message({
            type: "success",
            duration: 3000,
            message: "新增成功",
          });
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  created() {
    roleList({})
      .then((data) => {
        let _this = this;
        _this.$store.commit("setRoles", data);
      })
      .catch(function (error) {
        console.log(error);
      });
  },
  computed: {
    ...mapState(["roles"]),
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
